<template>
  <div class="login-container">
    <div class="login-left">
      <div class="brand-section">
        <div class="logo-container">
          <div class="logo-icon">⭐</div>
          <h1 class="brand-title">Sparkie<br>Interview</h1>
        </div>
        <p class="brand-subtitle">AI 驱动的智能面试平台</p>
      </div>
      <div class="decoration">
        <div class="shape shape-1"></div>
        <div class="shape shape-2"></div>
        <div class="shape shape-3"></div>
      </div>
    </div>
    
    <div class="login-right">
      <div class="login-form-container">
        <h2 class="form-title">欢迎回来</h2>
        
        <el-form
          ref="loginFormRef"
          :model="loginForm"
          :rules="loginRules"
          class="login-form"
          @submit.prevent="handleLogin"
        >
          <el-form-item prop="email">
            <el-input
              v-model="loginForm.email"
              placeholder="邮箱地址"
              prefix-icon="Message"
              size="large"
              class="form-input"
            />
          </el-form-item>
          
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              placeholder="密码"
              prefix-icon="Lock"
              size="large"
              show-password
              class="form-input"
            />
          </el-form-item>
          
          <el-form-item prop="role">
            <div class="role-selection">
              <div 
                class="role-card"
                :class="{ active: loginForm.role === 'hr' }"
                @click="loginForm.role = 'hr'"
              >
                <el-radio v-model="loginForm.role" value="hr" class="role-radio">
                  <span class="role-content">
                    <span class="role-text"><el-icon class="role-icon"><UserFilled /></el-icon>HR 招聘官</span>
                  </span>
                </el-radio>
              </div>
              <div 
                class="role-card"
                :class="{ active: loginForm.role === 'candidate' }"
                @click="loginForm.role = 'candidate'"
              >
                <el-radio v-model="loginForm.role" value="candidate" class="role-radio">
                  <span class="role-content">
                    <span class="role-text"><el-icon class="role-icon"><User /></el-icon>应聘者/候选人</span>
                  </span>
                </el-radio>
              </div>
            </div>
          </el-form-item>
          
          <el-form-item>
            <el-button
              type="primary"
              size="large"
              class="login-button"
              :loading="isLoading"
              @click="handleLogin"
            >
              登录体验
            </el-button>
          </el-form-item>
        </el-form>
        
        <div class="form-footer">
          <el-link type="primary" class="footer-link">忘记密码？</el-link>
          <el-link type="primary" class="footer-link" @click="goToRegister">注册账号</el-link>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '@/stores/auth'
import { ElMessage } from 'element-plus'
import { UserFilled, User } from '@element-plus/icons-vue'

export default {
  name: 'LoginView',
  components: {
    UserFilled,
    User
  },
  setup() {
    const router = useRouter()
    const authStore = useAuthStore()
    const loginFormRef = ref()
    const isLoading = ref(false)

    const loginForm = reactive({
      email: '',
      password: '',
      role: 'candidate'
    })

    const loginRules = {
      email: [
        { required: true, message: '请输入邮箱地址', trigger: 'blur' },
        { type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' }
      ],
      password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 6, message: '密码长度不能少于6位', trigger: 'blur' }
      ],
      role: [
        { required: true, message: '请选择角色', trigger: 'change' }
      ]
    }

    const handleLogin = async () => {
      try {
        const valid = await loginFormRef.value.validate()
        if (!valid) return

        // 验证数据完整性
        if (!loginForm.email || !loginForm.password || !loginForm.role) {
          ElMessage.error('请填写完整的登录信息')
          return
        }

        isLoading.value = true
        
        // 准备登录数据
        const loginData = {
          email: loginForm.email.trim(),
          password: loginForm.password.trim(),
          role: loginForm.role
        }
        
        console.log('准备登录，数据:', { ...loginData, password: '***' })
        
        await authStore.login(loginData)
        
        // 根据角色跳转到相应页面
        const redirectPath = loginForm.role === 'hr' ? '/hr/dashboard' : '/candidate/dashboard'
        router.push(redirectPath)
        
      } catch (error) {
        console.error('登录失败:', error)
        
        // 更详细的错误处理
        if (error.response?.status === 400) {
          const errorData = error.response.data
          if (typeof errorData === 'object') {
            const errors = []
            for (const [field, messages] of Object.entries(errorData)) {
              if (Array.isArray(messages)) {
                errors.push(`${field}: ${messages.join(', ')}`)
              } else {
                errors.push(`${field}: ${messages}`)
              }
            }
            ElMessage.error(`登录失败: ${errors.join('; ')}`)
          } else {
            ElMessage.error('登录失败: 请检查输入信息')
          }
        } else if (error.message) {
          ElMessage.error(`登录失败: ${error.message}`)
        } else {
          ElMessage.error('登录失败: 未知错误')
        }
      } finally {
        isLoading.value = false
      }
    }

    const goToRegister = () => {
      router.push('/register')
    }

    return {
      loginFormRef,
      loginForm,
      loginRules,
      isLoading,
      handleLogin,
      goToRegister
    }
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  min-height: 100vh;
}

.login-left {
  flex: 2;
  background: linear-gradient(135deg, var(--primary-color) 0%, var(--secondary-color) 100%);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: hidden;
}

.brand-section {
  text-align: center;
  color: white;
  z-index: 2;
}

.logo-container {
  margin-bottom: 24px;
}

.logo-icon {
  font-size: 4rem;
  margin-bottom: 16px;
}

.brand-title {
  font-size: 2.5rem;
  font-weight: 700;
  margin-bottom: 16px;
  line-height: 1.2;
}

.brand-subtitle {
  font-size: 1.2rem;
  opacity: 0.9;
  font-weight: 500;
}

.decoration {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 1;
}

.shape {
  position: absolute;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 50%;
}

.shape-1 {
  width: 300px;
  height: 300px;
  top: -150px;
  left: -150px;
}

.shape-2 {
  width: 200px;
  height: 200px;
  bottom: -100px;
  right: -100px;
}

.shape-3 {
  width: 150px;
  height: 150px;
  top: 50%;
  right: -75px;
  transform: translateY(-50%);
}

.login-right {
  flex: 3;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
  background-color: var(--bg-color);
}

.login-form-container {
  width: 100%;
  max-width: 400px;
}

.form-title {
  font-size: 2rem;
  font-weight: 600;
  color: var(--text-primary);
  margin-bottom: 32px;
  text-align: center;
}

.login-form {
  margin-bottom: 24px;
}

.form-input {
  margin-bottom: 16px;
}

.role-selection {
  display: flex;
  gap: 12px;
  width: 100%;
}

.role-card {
  flex: 1 ;
  border: 3px solid var(--border-color);
  border-radius: 8px;
  cursor: pointer;
  height: 50px;
  transition: all 0.3s ease;
}

.role-card:hover {
  border-color: var(--primary-color);
  background-color: rgba(103, 83, 192, 0.05);
}

.role-card.active {
  border-color: var(--primary-color);
  background-color: rgba(103, 83, 192, 0.1);
}

.role-radio {
  width: 100%;
  margin: 0;
  padding: 16px;
}

.role-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 5px;
  width: 100%;
}

.role-icon {
  font-size: 25px;
  color: var(--primary-color);
}

.role-text {
  font-size: 14px;
  color: var(--text-primary);
  font-weight: 500;
}

.login-button {
  width: 100%;
  height: 48px;
  font-size: 16px;
  font-weight: 600;
  background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
  border: none;
  border-radius: 8px;
}

.login-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 24px rgba(103, 83, 192, 0.3);
}

.form-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.footer-link {
  font-size: 14px;
  color: var(--primary-color);
  text-decoration: none;
}

.footer-link:hover {
  text-decoration: underline;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .login-container {
    flex-direction: column;
  }
  
  .login-left {
    flex: none;
    height: 300px;
  }
  
  .login-right {
    flex: none;
    padding: 24px;
  }
  
  .role-selection {
    flex-direction: column;
    gap: 8px;
  }
  
  .brand-title {
    font-size: 2rem;
  }
  
  .brand-subtitle {
    font-size: 1rem;
  }
}
</style> 